package flyingpig.servlet;

import java.util.Date;
import java.util.List;
import java.util.TimerTask;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Transaction;

import flyingpig.data.dao.JobPageHome;
import flyingpig.data.domain.JobPage;
import flyingpig.hibernate.HibernateUtil;

public class JobChecker extends TimerTask {
	private static Log log = LogFactory.getLog(JobChecker.class);
	private int hoursAgo = 4;

	public synchronized void run() {

		Transaction tx = HibernateUtil.getCurrentSession().beginTransaction();
		try {
			JobPageHome jobPageHome = new JobPageHome();
			List list = jobPageHome.findDeadPage(hoursAgo);

			for (int i = 0; i < list.size(); i++) {
				JobPage p = (JobPage) list.get(i);

				p.setPageState("INCOMPLETE");
				p.setLastUpdate(new Date());
			}
			tx.commit();

		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		}

	}
}
